package com.itheima.health.service.impl;

import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.exception.CustomizeException;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

@Slf4j
@DubboService
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;


    @Transactional
    @Override
    public void uploadExcelFile(ArrayList<OrderSetting> orderSettingArrayList) {
        for (OrderSetting orderSetting : orderSettingArrayList) {
            orderSettingDao.insertSelective(orderSetting);
        }
    }

    @Override
    public List<Map<String, Object>> getOrderSettingByMonth(Integer year, Integer month) {
        /*
         * @Date: 5:13 下午 2021/1/9
         * @param: [year, month]
         * @return: java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
         * @Description:
         * 1、组装SQL查数据存入list容器中
         * {date: 4, number: 200, reservations: 0},
         * 2、前端需要上面类型数据，所以需要创建map集合进行封装list
         * 3、返回整个大list容器
         */
        String beginData = String.format("%d-%d-%d", year, month, 1);
        String endData = String.format("%d-%d-%d", year, month, 31);

        //组装SQL
        Example example = new Example(OrderSetting.class);
        Example.Criteria criteria = example.createCriteria();
        //大于等于
        criteria.andGreaterThanOrEqualTo("orderDate", beginData);
        //小于等于
        criteria.andLessThanOrEqualTo("orderDate", endData);
        List<OrderSetting> orderSettingList = orderSettingDao.selectByExample(example);

        //组装map集合
        List<Map<String, Object>> orderSettingMapList = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettingList) {
            Map<String, Object> map = new HashMap<>();

            //pojo类要的是准确日期，数据库返回的是所有需要使用Calendar进行处理
            Date orderDate = orderSetting.getOrderDate();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(orderDate);
            map.put("date", calendar.get(Calendar.DAY_OF_MONTH));

            map.put("number", orderSetting.getNumber());
            map.put("reservations", orderSetting.getReservations() == null ? 0 : orderSetting.getReservations());
            orderSettingMapList.add(map);
        }

        return orderSettingMapList;
    }


    @Transactional//开启事务
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        /*
         * @Date: 8:54 下午 2021/1/9
         * @param: [orderSetting]
         * @return: void
         * @Description: 判断如果有对应的数据则更新，没有则新增（防止用户提交脏数据/重复数据）
         */

        log.info("[orderSetting表-预约设置-接收数据] - orderSetting: {}", orderSetting);

        //封装SQL条件
        Example example = new Example(OrderSetting.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("orderDate", orderSetting.getOrderDate());

        OrderSetting oneByExample = orderSettingDao.selectOneByExample(example);
        //根据传入数据查询数据库，数据为空则进行添加数据，否则更新
        if (oneByExample!=null){
            //获取上传的可预约人数
            Integer number = orderSetting.getNumber();
            //获取数据库中已预约人数
            Integer reservations = oneByExample.getReservations() == null ? 0 : oneByExample.getReservations();
            log.info("[预约-预约设置-editNumberByDate-service数据] - 可预约人数number: {} - 已预约人数: {}", number, reservations);
            //判断已预约人数大于设置可预约人数，抛出异常
            if (number < reservations) {
                throw new CustomizeException("设置数据错误，请检查数据");
            }
            orderSettingDao.updateByExampleSelective(orderSetting, example);
        }

        orderSettingDao.insertSelective(orderSetting);
    }

    @Transactional
    @Override
    public void add(ArrayList<OrderSetting> orderSettingArrayList) {
        /*
        * @Date: 8:58 下午 2021/1/9
        * @param: [orderSettingArrayList]
        * @return: void
        * @Description: 先判断是否可以更新，没有则新增（防止用户提交脏数据/重复数据）
        */
        for (OrderSetting orderSetting : orderSettingArrayList) {
            //封装SQL
            Example example = new Example(OrderSetting.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("orderDate", orderSetting.getOrderDate());
            //判断是否可以更新
            int updateCount = orderSettingDao.updateByExampleSelective(orderSetting, example);
            if (updateCount == 0) {
                orderSettingDao.insertSelective(orderSetting);
            }

        }
    }
}
